<?php

namespace app\common\model\Hotel;

use think\Model;

// 引入软删除的模型
use traits\model\SoftDelete;

class Room extends Model
{
    //在模型内部去引入软删除
    use SoftDelete;
    //
    protected $name = "hotel_room";

    //设置软删除的字段更新的时间字段
    protected $deleteTime = 'deletetime';

    protected $append = [
        'thumb_text',
        'flag_text',
        'service_text',
    ];


    public function getThumbTextAttr($value, $data){
        //获取到cdn的地址
        $cdnurl = config('site.cdnurl') ? config('site.cdnurl') : '';
        $cdnurl = trim($cdnurl, '/');
        
        // 多张图字符串结构
        $thumb = isset($data['thumb']) ? $data['thumb'] : '';

        if(empty($thumb))
        {
            $thumb = "/assets/img/hotel1.jpg";
        }else
        {
            $thumb = explode(',', $thumb);
            if(!empty($thumb))
            {
                $pic = '';

                foreach($thumb as $item)
                {
                    if(is_file(".".$item))
                    {
                        $pic = $item;
                        break;
                    }
                }

                if(empty($pic))
                {
                    $pic = "/assets/img/hotel1.jpg";
                }

                $thumb = $pic;
            }
        }

        return $cdnurl.$thumb;
    }

    public function getFlagTextAttr($value, $data)
    {
        $flag = isset($data['flag']) ? trim($data['flag']) : '';

        $list = explode(',', $flag);

        return $list;
    }

    public function getServiceTextAttr($value, $data)
    {
        $service = isset($data['service']) ? trim($data['service']) : '';

        $list = explode(',', $service);

        return $list;
    }
}
